package fightplan.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import fightplan.persistence.FilePersistence;
import fightplan.views.ClientView;

@SuppressWarnings("serial")
public class LoadSheetAction extends AbstractAction {

	private ClientView clientView;

	public LoadSheetAction(ClientView clientView) {
		super("Blatt laden");
		this.clientView = clientView;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setDialogTitle("Blatt Laden");
		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
			public boolean accept(File f) {
				return !f.isFile() || f.getName().endsWith(".page");
			}

			public String getDescription() {
				return "TRN (Turnier) (*.page)";
			}
		});
		if (fileChooser.showDialog(clientView, "Laden") == JFileChooser.APPROVE_OPTION) {
			try {
				clientView.getSheet().setValue(FilePersistence.loadSheet(fileChooser.getSelectedFile()));
			} catch (Exception x) {
				x.printStackTrace();
				JOptionPane
						.showMessageDialog(clientView,
								"File kann nicht gelesen. Es ist entweder kein Seitenfile\noder von einer anderen Version geschrieben worden.");
			}
		}
	}

}